package com.zzy.center.controller;


import com.zzy.center.entity.UcenterMember;
import com.zzy.center.entity.vo.RegisterVo;
import com.zzy.center.service.UcenterMemberService;
import com.zzy.commonutils.JwtUtils;
import com.zzy.commonutils.R;
import com.zzy.commonutils.ordervo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author KAZU
 * @since 2023-01-19
 */
@RestController
@RequestMapping("/ucenterservice/member")
@CrossOrigin
public class UcenterMemberController {

	@Autowired
	private UcenterMemberService memberService;

	/**
	 * 登录
	 * */
	@PostMapping("login")
	public R loginUser(@RequestBody UcenterMember member) {
		String token = memberService.login(member);
		return R.ok().data("token", token);
	}

	/**
	 * 注册
	 * */
	@PostMapping("register")
	public R registerUser(@RequestBody RegisterVo registerVo) {
		memberService.register(registerVo);
		return R.ok();
	}

	/**
	 * 根据token获取用户信息
	 * */
	@GetMapping("getMemberInfo")
	public R getMemberInfo(HttpServletRequest request) {
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		// 查询数据
		UcenterMember member = memberService.getById(memberId);
		return R.ok().data("userInfo", member);
	}

	/**
	 * 自用 -> 根据id获取用户信息
	 * */
	@GetMapping("getInfoUc/{id}")
	public UcenterMember getInfoUc(@PathVariable String id) {
		return memberService.getById(id);
	}

	/**
	 * 远程调用 -> 根据id获取用户信息
	 * */
	@PostMapping("getUserInfoOrder/{id}")
	public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
		UcenterMember member = memberService.getById(id);
		UcenterMemberOrder memberOrder = new UcenterMemberOrder();
		BeanUtils.copyProperties(member, memberOrder);
		return memberOrder;
	}
	
	/**
	 * 查询某一天的注册人数
	 * */
	@GetMapping("countRegister/{day}")
	public R countRegister(@PathVariable String day) {
		Integer count = memberService.countRegisterDay(day);
		return R.ok().data("countRegister", count);
	}
	
}




















